<?php

namespace app\api\controller\app\xht;

use app\admin\model\Banner;
use app\admin\model\City;
use app\admin\model\OrgNotice;
use app\admin\model\Shop;
use app\common\controller\Api;
use app\common\library\Ems;
use app\common\library\Sms;
use fast\Http;
use fast\Random;
use think\Config;
use think\Validate;

/**
 */
class Index extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = '*';

    public function _initialize()
    {
        parent::_initialize();

    }

    public function getOrgShop(){

        $orgId = $this->request->param('orgId');
        $shopModel = new Shop();

        $shop = $shopModel->where('org_id',$orgId)->find();
        if(!$shop){
            $this->error("此机构下暂无店铺,请联系技术方");
        }
        $bannerModel = new Banner();
        $banner = $bannerModel->where('org_id',$orgId)->find();
        $bannerImg = cdnurl($banner['image'],true);
        $cover = explode(",",$shop['cover']);
        $list = array();
        foreach ($cover as $key=>$val){
            if($val != ""){
                $img = cdnurl($val,true);
                $list[] = $img;
            }
        }
        $shop['cover'] = $list;
        $shop['banner'] = $bannerImg;
        $this->success('',$shop);
    }

    public function getCity(){

      $lat =   $this->request->param('lat');
      $lng = $this->request->param('lng');
      $orgId = $this->request->param('orgId');

      $location = $lat.",".$lng;

      $url = "https://api.map.baidu.com/reverse_geocoding/v3/?ak=IlnlhqFetsnmumPtjfSPEgoGSDjdqZTo&output=json&coordtype=wgs84ll&location=".$location;

      $result = Http::get($url);
      $result = json_decode($result,true);
      $ret = array();

      $orgNoticeModel = new OrgNotice();
      $notice = $orgNoticeModel->where('org_id',$orgId)->find();
      if($notice){
          $ret['notice'] = $notice['notice'];
      }else{
          $ret['notice'] = "";

      }
      if($result['status'] != 0){

          $cityModel = new City();
          $cityList = $cityModel->where('org_id',$orgId)->select();


          $ret['cityList'] = $cityList;
          $ret['cityName'] = "全国";

          $bannerModel = new Banner();
          $list = $bannerModel->where('org_id',$orgId)->select();
          $banners = array();
          foreach ($list as $key=>$val){
              $b = cdnurl($val['image'],true);
              $banners[]= $b;
          }
          $ret['banner'] = $banners;
          $this->success('',$ret);

      }else{

          $cityName = $result['result']['addressComponent']['city'];

          $cityModel = new City();
          $cityList = $cityModel->where('org_id',$orgId)->select();

          $ret['cityList'] = $cityList;
          $ret['cityName'] = $cityName;

          $bannerModel = new Banner();
          $list = $bannerModel->where('org_id',$orgId)->select();
          $banners = array();
          foreach ($list as $key=>$val){
              $b = cdnurl($val['image'],true);
              $banners[]= $b;
          }
          $ret['banner'] = $banners;

          $this->success('',$ret);

      }
    }

    public function getShopList(){

        $page = $this->request->param('page');
        $city = $this->request->param('city');
        $lat = $this->request->param('lat');
        $lng = $this->request->param('lng');
        $orgId =$this->request->param('orgId');
        $type = $this->request->param('type');

        $limit = 10;
        $offset = ($page - 1) * $limit;

        $where = "";
        if($type != ""){
            if($type == 'qp'){
                 $where = " and is_table=-1";
            }else{
                $where = " and is_table=1";

            }
        }

        if($city != ''){

            $sql = "SELECT id,name,address,logo,lng,status,lat,tel,is_table from q_shop where  city = '".$city."' and org_id = ".$orgId." and status != -1 $where LIMIT $offset ,$limit";
        }else{

            $sql = "select * from(
SELECT id,name,address,logo,lng,lat,tel,is_table,status,
ROUND(6378.138*2*ASIN(SQRT(POW(SIN(($lat*PI()/180-lat*PI()/180)/2),2)+COS($lat*PI()/180)*COS(lat*PI()/180)*POW(SIN(($lng*PI()/180-lng*PI()/180)/2),2)))*1000) AS distance
FROM q_shop where `status` !='-1' and org_id = $orgId $where) as tmp_table_name
order by distance LIMIT $offset ,$limit";
        }
        $shopModel = new Shop();
        $list = $shopModel->query($sql);

        error_log("==sql==".$sql);

        foreach ($list as $key=>$val){
            $list[$key]['status_txt'] = $shopModel->getStatus($val['status']);
            $list[$key]['logo'] = cdnurl($val['logo'],true);
            if(isset($val['distance'])){
                $list[$key]['distance'] = round($val['distance']/1000,2)."km";

            }else{
                $list[$key]['distance'] = "";

            }
            $bookTime = $this->redis->get($val['id']);
            if($bookTime != null){
                $list[$key]['bookTime'] = time_ago2(date("Y-m-d H:i:s",$bookTime))."有人预定";
            }else{
                $list[$key]['bookTime'] = "";
            }
        }
        $this->success('',$list);


    }




    public function search(){

        $page = $this->request->param('page');
        $lat = $this->request->param('lat');
        $lng = $this->request->param('lng');
        $keyword = $this->request->param('keyword');
        $orgId = $this->request->param('orgId');

        $limit = 10;
        $offset = ($page - 1) * $limit;


        $where = " where org_id=".$orgId;
        if($keyword != ''){
          $where .=  " and name like '%".$keyword."%'";
        }

        if($lat == ''){
            $sql = "SELECT id,name,address,logo,lng,lat,tel from q_shop $where  LIMIT $offset ,$limit";

        }else{
            $sql = "select * from(
SELECT id,name,address,logo,lng,lat,tel,
ROUND(6378.138*2*ASIN(SQRT(POW(SIN(($lat*PI()/180-lat*PI()/180)/2),2)+COS($lat*PI()/180)*COS(lat*PI()/180)*POW(SIN(($lng*PI()/180-lng*PI()/180)/2),2)))*1000) AS distance
FROM q_shop  $where LIMIT $offset ,$limit)  as tmp_table_name
order by distance";
        }

        $shopModel = new Shop();
        $list = $shopModel->query($sql);


        foreach ($list as $key=>$val){
            $list[$key]['logo'] = cdnurl($val['logo'],true);
            if(isset($val['distance'])){
                $list[$key]['distance'] = round($val['distance']/1000,2)."km";

            }else{
                $list[$key]['distance'] = "";
            }
            $bookTime = $this->redis->get($val['id']);
            if($bookTime != null){
                $list[$key]['bookTime'] = time_ago($bookTime)."有人预定";
            }else{
                $list[$key]['bookTime'] = "";
            }
        }
        $this->success('',$list);


    }


}
